<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;
class Index extends Controller
{
	//无限分类递归数据遍历
	public function getcatesbypid($pid){
		$data=Db::table("cates")->where("pid","{$pid}")->select();
		$data1=array();
		//遍历$data
		foreach($data as $value){
			//获取子类信息存入shop字段
			$value['shop']=$this->getcatesbypid($value['id']);
			$data1[]=$value;
		}
		return $data1;
	}
    public function getindex()
    {
    	// var_dump(Session::get('uid'));
		//轮播图数据遍历
		$carousel=Db::query("select pic from carousel where status=0");
		//公告遍历
		$time=time();
		$notice=Db::query("select id,title from notice where $time between begintime and endtime;");
		//广告遍历
		$advert=Db::query("select pic,url from advert where status=0");
    	//品牌遍历
    	$brand=Db::table("brand")->where('status','0')->select();
    	// var_dump($brand);exit;
		//友情链接遍历
		$links=Db::query("select linksname,linksaddress from links where status=0");
		
    	//调用递归函数
    	$cate=$this->getcatesbypid(0);
    	// echo '<pre>';
    	// print_r($cate);exit;
    	// var_dump($cate);exit;
    	// 商品遍历
    	$request=request();
    	//获取cid
    	$cid=$request->get("cid");
    	//判断是否为Ajax请求
    	if(!$request->isAjax()){
			//商品遍历
			// $goods=Db::table("pro_goods pro")->join("cates cat","pro.cate_id=cat.id and pro.status=0")->select();
			// var_dump($goods);exit;
			$goods=Db::query("select id,name,price,pic,cate_id from pro_goods where status=0");
	  		// var_dump($goods);exit;
    		//引入前台首页
        	return $this->fetch("Index/index",['cate'=>$cate,'carousel'=>$carousel,'notice'=>$notice,'advert'=>$advert,'links'=>$links,'goods'=>$goods,'brand'=>$brand]);	
    	}
    	//是Ajax请求
    	// echo $cid;
    	//获取当前一级分类下商品
    	// $res=Db::table("pro_goods")->field("pro_goods.pic,pro_goods.price,pro_goods.id as pid,pro_goods.name as pname,cates.id as cid,cates.name as cname")->join("cates","pro_goods.cates_id=cates.id")->where("pro_goods.cates_id","{$cid}")->select();
    	// echo json_encode($res);
    }
    //公告模块
    public function getnotice(){
    	$request=request();
    	$id=$request->param('id');
    	$notice=Db::query("select * from notice where id=$id");
    	$notice[0]['begintime']=date("Y-m-d H:i:s",$notice[0]['begintime']);
    	$notice[0]['endtime']=date("Y-m-d H:i:s",$notice[0]['endtime']);
    	return $this->fetch("/Index/notice",['notice'=>$notice]);	
    }
    //商品搜索模块
    public function postgoods(){
		$request=request();
		$name=$request->param('search');
		//搜索值为空则令$goods为空
		if($name){
			//模糊搜索商品
			$goods=Db::query("select id,name,price,pic from pro_goods where name like '%{$name}%'");
	    }else{
	    	$goods='';
	    }
	    return $this->fetch("Index/goods",['goods'=>$goods,'name'=>$name]);
    }
    //无限分类查询商品
    public function getcategory(){
		$request=request();
		$id=$request->param('id');
		$name=Db::query("select name from cates where id={$id}")[0]['name'];
		//模糊搜索商品
		$goods=Db::query("select id,name,price,pic from pro_goods where cate_name like '%{$name}%'");
		return $this->fetch("Index/goods",['goods'=>$goods,'name'=>$name]);
    }

     //品牌搜索模块
    public function getbrand(){
		$request=request();
		$id=$request->param('id');
		$name=Db::query("select brand from brand where id={$id}")[0]['brand'];
		$goods=Db::query("select id,name,price,pic from pro_goods where status=0 and brand_id={$id}");
		// var_dump($brand);exit;
	    return $this->fetch("Index/brand",['goods'=>$goods,'name'=>$name]);
    }
}